package com.yao.yaoojbackendjudgeservice.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yao.yaoojbackendmodel.model.codesandbox.JudgeInfo;
import com.yao.yaoojbackendmodel.model.dto.question.JudgeCase;
import com.yao.yaoojbackendmodel.model.dto.question.JudgeConfig;
import com.yao.yaoojbackendmodel.model.entity.Question;
import com.yao.yaoojbackendmodel.model.enums.JudgeInfoMessageEnum;

import java.util.List;
import java.util.Optional;

/**
 * JAVA程序判题策略
 */
public class JavaLanguageJudgeStrategy implements JudgeStrategy {

    /**
     * 执行判题
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {

        List<String> outputList = judgeContext.getOutputList();
        List<String> inputList = judgeContext.getInputList();
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        Question question = judgeContext.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L); //得到程序运行内存 默认为0
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);//得到程序运行时间 默认为0

        //5.根据沙箱的执行结果来判断题目的状态和信息
        JudgeInfoMessageEnum judgeInfoMessageEnum=JudgeInfoMessageEnum.WAITING;//judgeInfo：提交信息 WAITING 等待中
        JudgeInfo judgeInfoResponse=new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());

        //5.1判断沙箱执行的结果输出数量是否和预期输出数量相等
        if(outputList.size()!=inputList.size())
        {
            judgeInfoMessageEnum=JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        //5.2依次判断每一项输出和预期输出是否一致
        for(int i=0;i<judgeCaseList.size();i++){
            JudgeCase judgeCase = judgeCaseList.get(i);
            if(!judgeCase.getOutput().equals(outputList.get(i))){
                judgeInfoMessageEnum=JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }
        //5.3判断题目的限制是否符合要求
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Long needtimeLimit = judgeConfig.getTimeLimit();
        Long needmemoryLimit = judgeConfig.getMemoryLimit();
        if(memory > needmemoryLimit){
            judgeInfoMessageEnum=JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        //JAVA程序本身需要额外执行10秒钟
        Long JAVA_PROGRAM_TIME_COST=10000L;
        if((time - JAVA_PROGRAM_TIME_COST) > needtimeLimit){
            judgeInfoMessageEnum=JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        judgeInfoMessageEnum=JudgeInfoMessageEnum.ACCEPTED;
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;
    }
}
